package com.demo.test.business;

import org.junit.Test;

import java.text.NumberFormat;

/**
 * @author gy
 * @date 2023/3/16
 */
public class AddZero {

    @Test
    public void m1(){
        String createdNum = "1231";
        createdNum = this.addZeroForNum(createdNum,6);
        System.out.println(createdNum);
    }
    /**
     * 自动获取字符串中的数值，并按照指定位数进行自动补零
     * @param numberStr 数字字符串 "00123"
     * @param digits 补零位数
     * @example (0123,6) -> 000123
     * @return
     */
    public String addZeroForNum(String numberStr,int digits){
        int number = Integer.parseInt(numberStr);
        NumberFormat formatter = NumberFormat.getNumberInstance();
        formatter.setMinimumIntegerDigits(digits);
        formatter.setGroupingUsed(false);
        return formatter.format(number);
    }

    /**
     * 截取规格的字符串
     */
    @Test
    public void m6(){
        String existLoginId = "BJ2023000156";
        System.out.println(existLoginId.substring(0, 2));
        System.out.println(existLoginId.substring(2, 6));
        System.out.println(existLoginId.substring(6, 12));
    }
}
